package com.homework.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.homework.bean.CourseBean;
import com.homework.bean.InformBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.UserBean;
import com.homework.bean.UserInformBean;
import com.homework.bean.UserInformListBean;
import com.homework.service.PageInfoService;
import com.homework.util.connDB;

public class InformDao {

	private static InformDao informDao;
	private connDB conn = new connDB();
	private ResultSet rs;
	private PageInfoService pageInfoService;
	// sql语句
	private String sql = "";

	public InformDao() {
		pageInfoService = new PageInfoService();
	}

	public static InformDao getInstance() {
		if (informDao == null) {
			informDao = new InformDao();
		}
		return informDao;
	}

	// 通知列表
	public UserInformListBean informShow(CourseBean courseBean,
			PageInfoBean pageInfoBean) throws SQLException {
		// 数据库连接
		sql = "select f_username,t_inform.* from t_inform natural join (select f_userName,f_chooseId from t_user natural join (select f_chooseId,f_userId from t_choose where f_courseId = "
				+ courseBean.getCourseId()
				+ ") as t_chooseTemp) as t_userTemp;";
		rs = conn.executeQuery(sql);

		// --------------分页基本参数获取---------------
		pageInfoBean.setRs(rs);
		pageInfoService.setPageInfoBean(pageInfoBean);

		// 获取UserInformListBean
		UserInformListBean userInformListBean = new UserInformListBean();
		for (int i = 0; i < pageInfoBean.getNum(); i++) {
			UserInformBean userInformBean = new UserInformBean();
			InformBean informBean = new InformBean();
			UserBean userBean = new UserBean();
			informBean.setInformId(rs.getString("f_informId"));
			informBean.setInformHead(rs.getString("f_informHead"));
			informBean.setInformBody(rs.getString("f_informBody"));
			informBean.setCreationTime(rs.getString("f_creationTime"));
			informBean.setInformStatus(rs.getString("f_informStatus"));
			informBean.setChooseId(rs.getString("f_chooseId"));
			userBean.setUsername(rs.getString("f_username"));
			userInformBean.setInformBean(informBean);
			userInformBean.setUserBean(userBean);
			userInformListBean.getUserInformList().add(userInformBean);
			rs.next(); // 下一条记录
		}

		rs.close();
		conn.close();
		return userInformListBean;
	}

	// 发布通知
	public void informAdd(CourseBean courseBean, UserBean userBean,
			InformBean informBean) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		sql = "insert into t_inform (f_informHead,f_informBody,f_creationTime,f_informStatus,f_chooseId) values ('"
				+ informBean.getInformHead()
				+ "','"
				+ informBean.getInformBody()
				+ "','"
				+ df.format(new Date())
				+ "',1,(select f_chooseId from t_choose where f_userId = "
				+ userBean.getUserId()
				+ " and f_courseId = "
				+ courseBean.getCourseId() + "));";
		conn.executeUpdate(sql);
		conn.close();
	}
}
